package com.cos.students.web.controller;


import com.cos.students.common.Const;
import com.cos.students.common.ResponseResult;
import com.cos.students.entity.RcStudent;
import com.cos.students.services.IRcStudentService;
import com.cos.students.util.DateUtil;
import com.cos.students.vo.input.SearchChoseCourseVO;
import com.cos.students.vo.output.LoginInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cos
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/rc-student")
public class RcStudentController {
    @Autowired
    private IRcStudentService studentService;

    @PostMapping("/updateStudent")
    public ResponseResult updateStudent(HttpSession session, String studentEmail, String studentBirthday, String studentPassword) {
        LoginInfoVo info = (LoginInfoVo) session.getAttribute(Const.IS_LOGIN);
        RcStudent stu  = new RcStudent(info.getUserId(),studentPassword,studentEmail, DateUtil.stringToDate(studentBirthday));
        return studentService.updateStudent(stu);
    }

    @RequestMapping("/searchCourse")
    public ResponseResult searchCourse(SearchChoseCourseVO courseVO, HttpSession session) {
        LoginInfoVo info = (LoginInfoVo) session.getAttribute(Const.IS_LOGIN);
        //封装登录学员的id进查询的数据对象SearchChoseCourseVO input vo
        courseVO.setStudentId(info.getUserId());
        return studentService.searchCourse(courseVO);
    }

    @GetMapping("/getStudentInfo")
    public ResponseResult getStudentInfo(HttpSession session) {
        //获取登录用户信息
        LoginInfoVo infoVo = (LoginInfoVo) session.getAttribute(Const.IS_LOGIN);
        //访问业务层获取用户数据
        return studentService.getStudentInfo(infoVo.getUserId());
    }


}
